package com.hhit.bigscreenbackmanage.service.impl.video;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.PageUtil;
import com.hhit.bigscreenbackmanage.dao.video.VideoMapper;
import com.hhit.bigscreenbackmanage.entity.video.VideoEntity;
import com.hhit.bigscreenbackmanage.service.video.VideoService;
import com.hhit.bigscreenbackmanage.service.wlwsp.WlwspService;
import com.hhit.bigscreenbackmanage.vo.AdminVideoUpdateStatusVO;
import com.hhit.bigscreenbackmanage.vo.DeleteVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements VideoService {

    private static Logger log = LoggerFactory.getLogger(VideoServiceImpl.class);

    private static Map<String,Object> param = new HashMap<>();

    @Autowired
    private WlwspService wlwspService;

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public PageUtil<VideoEntity> getVideoList(JSONObject jsonObject) throws Exception {
        PageUtil<VideoEntity> page = new PageUtil<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", jsonObject.get("pageSize"));
        map.put("pageNo", jsonObject.get("pageNo"));
        map.put("type", jsonObject.get("type"));
        page.pageCount=Integer.parseInt(map.get("pageSize").toString());
        page.init(videoMapper.getVideoCount(map),Integer.parseInt(map.get("pageNo").toString()));//参数1：总行数，参数2：当前页
        List<VideoEntity> resultList = videoMapper.getVideoList(map);
        page.setList(resultList);
        return page;
    }

    @Override
    public int saveVideo(VideoEntity videoEntity) throws Exception {
        log.info("VideoServiceImpl.saveVideo Call Begin,param[videoEntity:{}]",videoEntity);
        if (ObjectUtil.isNull(videoEntity)){
            return -1;
        }
        // videoEntity.setId(IdUtil.fastSimpleUUID());
        return videoMapper.saveVideo(videoEntity);
    }


    @Override
    public int editVideo(VideoEntity videoEntity) throws Exception {
        log.info("VideoServiceImpl.editVideo Call Begin,param[videoEntity:{}]",videoEntity);
        if (ObjectUtil.isNull(videoEntity)){
            return -1;
        }

        //组织参数查询当前对象原有值
        Map<String,Object> queryParam=new HashMap<>();
        queryParam.put("gsbh",videoEntity.getGsbh());
        List<VideoEntity> list=videoMapper.getVideoList(queryParam);

        if(list.size() == 0){
            return videoMapper.editVideo(videoEntity);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("id",videoEntity.getId());
        VideoEntity videoResult=videoMapper.getVideoList(map).get(0);
        int i = videoMapper.editVideo(videoEntity);
        if(i>0){
            // 赋值修改关联对象
            VideoEntity entity2=new VideoEntity();
            entity2.setId(list.get(0).getId());
            entity2.setGsbh(videoResult.getGsbh());
            int result=videoMapper.editVideo(entity2);
            if(result>0){
                return result;
            }
        }
        return 0;
    }

    @Override
    public int removeVideo(DeleteVo deleteVo) throws Exception {
        log.info("VideoServiceImpl.removeVideo Call Begin,param[deleteVo:{}]",deleteVo);
        if (ObjectUtil.isNull(deleteVo)){
            return -1;
        }
        return videoMapper.removeVideo(deleteVo.getIds());
    }

    @Override
    public Map<String, Object> loadDefaultVideo() throws Exception {
        Map<String,Object> result = new HashMap<>();
        param.put("type",1);
        // List<FxsbEntity> fxsbEntityList = aqglMapper.getDeviceEwmbhByType(param);
        List<VideoEntity> VideoEntityList = videoMapper.getVideoList(param);
        if (CollectionUtil.isNotEmpty(VideoEntityList)){
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (int i = 0 ; i < VideoEntityList.size() ; i++){
                Map<String,Object> resMap = new HashMap<>();
                String rtspUrl = "rtsp://";
                try {
                    rtspUrl = wlwspService.getDevicePreviewurls(VideoEntityList.get(i).getEwmbh(),null);
                }catch (Exception e){
                    log.error("loadDefaultVideo==>查询视频流异常",e);
                }
                resMap.put("cameraIndexCode",VideoEntityList.get(i).getEwmbh());
                resMap.put("streamMode",0);
                resMap.put("cameraName",VideoEntityList.get(i).getEwmmc());
                resMap.put("url",rtspUrl);
                resMap.put("deviceType",4);
                resMap.put("smallEagleEyeAbility",0);
                resMap.put("gpuMode",0);
                resMap.put("wndId",i+1);
                resultList.add(resMap);
            }
            result.put("lists",resultList);
        }else {
            return new HashMap<>();
        }
        return result;
    }

    @Override
    public Map<String, Object> loadSafeDefaultVideo(JSONObject jsonObject) throws Exception {
        Map<String,Object> result = new HashMap<>();
        String param = jsonObject.get("param").toString();
        String[] ewmbhs = param.split(",");
        if (ewmbhs.length>0){
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (int i = 0 ; i < ewmbhs.length ; i++){
                String ewmbh = ewmbhs[i];
                if("".equals(ewmbh)){
                    continue;
                }else {
                    Map<String,Object> resMap = new HashMap<>();
                    String rtspUrl = "rtsp://";
                    try {
                        rtspUrl = wlwspService.getDevicePreviewurls(ewmbh,null);
                    }catch (Exception e){
                        log.error("loadDefaultVideo==>查询视频流异常",e);
                    }
                    resMap.put("cameraIndexCode",ewmbh);
                    resMap.put("streamMode",0);
                    resMap.put("cameraName","");
                    resMap.put("url",rtspUrl);
                    resMap.put("deviceType",4);
                    resMap.put("smallEagleEyeAbility",0);
                    resMap.put("gpuMode",0);
                    resMap.put("wndId",i+1);
                    resultList.add(resMap);
                }
            }
            result.put("lists",resultList);
        }else {
            return new HashMap<>();
        }
        return result;
    }


    @Override
    public Map<String, Object> loadMainEqVideo() throws Exception {
        Map<String,Object> result = new HashMap<>();
        param.put("type",2);
        // List<FxsbEntity> fxsbEntityList = aqglMapper.getDeviceEwmbhByType(param);
        List<VideoEntity> VideoEntityList = videoMapper.getVideoList(param);
        if (CollectionUtil.isNotEmpty(VideoEntityList)){
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (int i = 0 ; i < VideoEntityList.size() ; i++){
                Map<String,Object> resMap = new HashMap<>();
                String rtspUrl = "rtsp://";
                try {
                    rtspUrl = wlwspService.getDevicePreviewurls(VideoEntityList.get(i).getEwmbh(),null);
                }catch (Exception e){
                    log.error("loadMainEqVideo==>查询视频流异常",e);
                }
                resMap.put("cameraIndexCode",VideoEntityList.get(i).getEwmbh());
                resMap.put("streamMode",0);
                resMap.put("cameraName",VideoEntityList.get(i).getEwmmc());
                resMap.put("url",rtspUrl);
                resMap.put("deviceType",4);
                resMap.put("smallEagleEyeAbility",0);
                resMap.put("gpuMode",0);
                resMap.put("wndId",i+1);
                resultList.add(resMap);
            }
            result.put("lists",resultList);
        }else {
            return new HashMap<>();
        }
        return result;
    }

    @Override
    public void updateStatus(AdminVideoUpdateStatusVO adminVideoUpdateStatus) {
        videoMapper.updateStatus(adminVideoUpdateStatus.getId(), adminVideoUpdateStatus.getStatus());
    }


}
